<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <page-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :query="filterInfo.query"
        :tab-index="true"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :pager="false"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-licenseNumber="scope">
          <p>{{ scope.row.licenseNumber || '-' }}</p>
          <p>{{ scope.row.brandName || '-' }}</p>
        </template>
        <template v-slot:col-commonName="scope">
          <p>{{ scope.row.commonName || '-' }}</p>
          <p>{{ scope.row.productCode || '-' }}</p>
          <p>{{ scope.row.alias || '-' }}</p>
        </template>
        <template v-slot:col-type="scope">
          <p>{{ scope.row.productTypeValue || '-' }}</p>
          <p>{{ scope.row.codeTypeValue || '-' }}</p>
          <p>{{ scope.row.productCategoryValue || '-' }}</p>
        </template>
        <template v-slot:col-standard="scope">
          <p>{{ scope.row.standard || '-' }}</p>
          <p>{{ scope.row.unitName || '-' }}</p>
        </template>
      </page-table>
    </page-dialog>
  </div>
</template>

<script setup name="SelectSurgeryProduct">
import PageDialog from '@/components/PageDialog/index'
import PageTable from '@/components/PageTable/index'
import PageFilter from '@/components/PageFilter/index'
import { withTableReceivingOrderDetailMergedAll } from '@/api/stock'
import _ from 'lodash'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: withTableReceivingOrderDetailMergedAll,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  tableData: {
    type: Array,
    default: function () {
      return []
    },
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    ...props.query,
    searchType: 'product',
    searchField: 'productId',
  },
  list: [
    {
      type: 'input',
      label: '名称/别名/编码/助记符',
      value: 'keyword',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '通用名称\n编码\n别名', value: 'commonName', type: 'slot' },
    {
      label: '产品类型\n条码类型\n产品分类',
      value: 'type',
      type: 'slot',
    },
    { label: '规格\n计量单位', value: 'standard', type: 'slot' },
    {
      label: '消耗数量',
      value: 'consumeQty',
      type: 'column',
      formatter: $fn.qtyFormatter,
    },
    {
      label: '消耗单价',
      value: 'consumePrice',
      type: 'column',
      formatter: $fn.TripartiteMethod,
    },
    { label: '消耗时间', value: 'consumeTime' },
    { label: '证号\n生产厂家', value: 'licenseNumber', type: 'slot' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择跟台产品',
  width: '70%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
})
// 获取列表
function getList() {
  props.api(filterInfo.value.query).then((res) => {
    if (res) {
      const data = res.data.map((item) => {
        return {
          ...item,
          differenceByIdAddPrice: `${item.productId}A${item.consumePrice}`,
        }
      })
      if (data.length && props.tableData.length) {
        tableInfo.value.data = _.differenceBy(
          data,
          props.tableData,
          'differenceByIdAddPrice'
        )
      } else {
        tableInfo.value.data = data
      }
    }
  })
}
function search() {
  getList()
}
function save() {
  if (selectRow.value) {
    handleClose()
    emits('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择部门',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      // if(data.length) {
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value.length
      // }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped></style>
